import { location, session, StorageApi } from "@/util/storage";
import { encrypt, decrypt } from "@/util/CrtptoJS";

export interface StorageStore {
	sessionStorage: StorageApi;
	localStorage: StorageApi;
}

// development
// production

const storage: StorageStore = {
	sessionStorage:
		import.meta.env.MODE === "development"
			? window.sessionStorage
			: {
					getItem: key => {
						// return session.getItem(key);
						return decrypt(session.getItem(key) || "");
					},
					setItem: (key, value) => {
						// session.setItem(key, value);
						return session.setItem(key, encrypt(value.toString()));
					},
					removeItem: key => {
						return session.removeItem(key);
					},
			  },
	localStorage:
		import.meta.env.MODE === "development"
			? window.localStorage
			: {
					getItem: key => {
						// return location.getItem(key);
						return decrypt(location.getItem(key) || "");
					},
					setItem: (key, value) => {
						// return location.setItem(key, value);
						return location.setItem(key, encrypt(value.toString()));
					},
					removeItem: key => {
						return location.removeItem(key);
					},
			  },
};
export default storage;
